<?php

class ItsStockOutHead extends AppModel {
    var $name = 'ItsStockOutHead';

    const STOCK_OUT_PREFIX = 'STO';

    var $belongsTo = array(
            'Warehouse' => array(
                'fields' => array('id', 'name')
        ),
            'User' => array(
                'foreignKey' => 'collector_id',
                'fields' => array('username', 'name')
        )
    );

    var $displayField = 'stock_out_no';

    var $hasMany = array(
        'ItsStockOut' => array(
            'dependent'    => true
        )
    );

    var $validate = array(
            'stock_out_date' => array(
                    'rule' => array('notEmpty'),
                    'message' => 'Stock out date is required.',
                    'required' => true,
        ),
            'stock_out_no' => array(
                        'rule' => array('notEmpty'),
                        'message' => 'Stock out no. is required.',
                        'required' => true,
        ),
            'warehouse_id' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Warehouse is required.',
                    'required' => true,
        ),
    );

    function afterFind($results) {
        foreach ($results as &$s) {
            if (isset($s['ItsStockOutHead']['stock_out_date'])) {
                $s['ItsStockOutHead']['stock_out_date'] = $this->dateFormatAfterFind($s['ItsStockOutHead']['stock_out_date']);
            }
        }
        return $results;
    }


    /**
     * Custom Query สร้าง Query มาเองให้เป็นรูปแบบที่เหมาะสมสำหรับหน้า print
     * @param int stock_out_id $id
     * @return array data
     */
    function findForPrintByid($id) {
        $this->contain(array('Warehouse', 'User',
                'ItsStockOut' => array('Product' => array('part_no', 'text', 'Unit'))
            )
        );
        $data = $this->findById($id);

        $this->ItsStockOut->bindModel(array(
                'hasMany' => array(
                    'ProductSerial' => array(
                        'foreignKey' => 'its_stock_out_id',
                        'conditions' => array(
                            'its_stock_out_type' => ItsStockOut::ITS_STOCK_OUT_TYPE
                        )
                    )
                )
            )
        );

        $Product = $this->ItsStockOut->Product;
        $PS = $this->ItsStockOut->ProductSerial;
        $Location = $PS->Location;

        // วนลูปรายการ ItsStockOut
        foreach($data['ItsStockOut'] as &$is) {
            // หา Location ที่มีอยู่ใน ItsStockOut รายการนี้
            $psLocations = $PS->find('all', array(
                    'fields' => array('DISTINCT location_id'),
                    'conditions' => array(
                        'its_stock_out_id' => $is['id'],
                        'its_stock_out_type' => ItsStockOut::ITS_STOCK_OUT_TYPE,
                        'product_id' => $is['product_id']
                    )
                )
            );
            
            // แปะ Location ที่ได้เข้า ItsStockOut
            foreach ($psLocations as $psLocation) {
                $Location->contain();
                $location = $Location->findById($psLocation['ProductSerial']['location_id']);

                // option สำหรับ query product serial
                $options = array(
                    'contain' => array(),
                    'conditions' => array(
                        'its_stock_out_id' => $is['id'],
                        'its_stock_out_type' => ItsStockOut::ITS_STOCK_OUT_TYPE,
                        'product_id' => $is['product_id'],
                        'location_id' => $psLocation['ProductSerial']['location_id']
                    )
                );
                
                // นับจำนวนที่ stock out ใน location นี้
                $ps_count = $PS->find('count', $options);

                // ถึง เฉพาะ Product Serial ที่มี Serial no หรือ other information (ถ้าไม่มีจะออกมา 1 แถว)
                $options['fields'] = array('DISTINCT serial_no', 'other_information');
                $ps = $PS->find('all', $options);

                $location['stock_out_qty'] = $ps_count;
                $location['PS'] = $ps;

                $is['ProductLocation'][] = $location;
            }
        }
        return $data;
    }
}
?>
